package com.jyw.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.jyw.common.utils.DateUtils;
import com.jyw.system.domain.SysConfig;
import com.jyw.system.domain.vo.SysAlarmPromptConfigVo;
import com.jyw.system.enums.OtherConfigEnum;
import com.jyw.system.service.ISysAlarmPromptConfigService;
import com.jyw.system.service.ISysConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Created by ZhangCheng on 2023/08/16 (系统工单告警提示配置)
 */
@Service
public class SysAlarmPromptConfigServiceImpl implements ISysAlarmPromptConfigService {

    @Resource
    private ISysConfigService sysConfigService;

    @Override
    public SysAlarmPromptConfigVo getConfig() {
        SysConfig sysConfig = sysConfigService.findConfigByKey(OtherConfigEnum.ALARM_PROMPT.getKey());
        if (sysConfig != null && StrUtil.isNotEmpty(sysConfig.getConfigValue())) {
            return JSONObject.parseObject(sysConfig.getConfigValue(), SysAlarmPromptConfigVo.class);
        }
        return null;
    }

    @Override
    public SysAlarmPromptConfigVo getDefaultConfig() {
        SysAlarmPromptConfigVo configVo = getConfig();
        if (configVo != null) {
            return configVo;
        }
        return new SysAlarmPromptConfigVo();
    }

    @Override
    public void updateConfig(SysAlarmPromptConfigVo sysAlarmPromptConfigVo) {
        SysConfig sysConfig = sysConfigService.findConfigByKey(OtherConfigEnum.ALARM_PROMPT.getKey());
        String configValue = JSONObject.toJSONString(sysAlarmPromptConfigVo);
        if (sysConfig != null) {
            sysConfig.setConfigValue(configValue);
            sysConfig.setUpdateTime(DateUtils.getNowDate());
            sysConfigService.updateConfig(sysConfig);
        } else {
            sysConfigService.initConfig(configValue, OtherConfigEnum.ALARM_PROMPT);
        }
    }

}
